package net.scrumtool.view;

import java.awt.Event;

import net.scrumtool.technics.Utils;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;

public class SplashScreen {

	protected Shell splash;

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		splash.open();
		splash.layout();
		while (!splash.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * @wbp.parser.entryPoint
	 */
	public void init() {
		createContents();
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		final Display display = new Display();

		final int[] count = new int[] { 2 };

		final Image image = new Image(display, 100, 100);
		final Shell splash = new Shell(SWT.ON_TOP);
		splash.setSize(402, 117);
		Color splashColor = new Color(null, 65, 93, 110);
		splash.setBackground(splashColor);
		splash.setLayout(null);
		final ProgressBar bar = new ProgressBar(splash, SWT.NONE);
		bar.setBounds(0, 105, 424, 5);
		bar.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		bar.setMaximum(count[0]);

		bar.setVisible(true);

		bar.setVisible(false);

		splash.pack();
		Rectangle splashRect = splash.getBounds();
		Rectangle displayRect = display.getBounds();
		int x = (displayRect.width - splashRect.width) / 2;
		int y = (displayRect.height - splashRect.height) / 2;
		splash.setLocation(x, y);

		Composite compositeLogo = new Composite(splash, SWT.NONE);
		compositeLogo.setBounds(310, 10, 108, 89);

		compositeLogo.setBackgroundImage(Utils.getImage(Utils.IMG_LOGO));

		Composite compositeNom = new Composite(splash, SWT.NONE);
		compositeNom.setBounds(10, 10, 294, 89);

		//compositeNom.setBackgroundImage(Utils.getImage(Utils.IMG_NAME));

		splash.open();
		display.asyncExec(new Runnable() {
			@Override
			public void run() {
				Shell[] shells = new Shell[count[0]];
				for (int i = 0; i < count[0]; i++) {
					shells[i] = new Shell(display);
					shells[i].setSize(300, 300);
					shells[i].addListener(SWT.Close, new Listener() {
						public void handleEvent(Event e) {
							--count[0];
						}

						@Override
						public void handleEvent(org.eclipse.swt.widgets.Event arg0) {
							// TODO Auto-generated method stub

						}
					});
					bar.setSelection(i + 1);
					try {
						Thread.sleep(1000);
					} catch (Throwable e) {
					}
				}
				splash.close();
				image.dispose();

				Home home = new Home();
				home.open();

			}
		});
		while (count[0] != 0) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
}
// Flo
